﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace yaai
{
    public partial class grunkaListController : UserControl
    {


        public bool isHorizontal;
        List<grunka> grunkor;
        public List<WordList> lists;

        public event EventHandler changeOrientation;

        public grunkaListController(List<WordList> lists)
        {
            isHorizontal = true;
            this.lists = lists;
            grunkor = new List<grunka>();
            InitializeComponent();
        }

        private void btn_addFirst_Click(object sender, EventArgs e)
        {
            addGrunkaAtOrder(-1);
        }

        public void addGrunka()
        {
            addGrunka(-1);
        }

        public String genereteTextMessage()
        {
            StringBuilder sb = new StringBuilder();
            foreach (grunka g in grunkor)
            {
                sb.Append(g.getWordOrListName());
                sb.Append(" ");
            }
            return sb.ToString();
        }

        public List<simpleGrunka> getSimpleGrunkor()
        {
            List<simpleGrunka> result = new List<simpleGrunka>();
            foreach (grunka g in grunkor)
            {
                result.Add(g.toSimpleGrunka());
            }
            return result;
        }

        private void addGrunka(int id)
        {
            grunka g;
            if (id == -1)
            {
                g = new grunka(this, grunkor.Count);
                g.Location = calcGrunkLocation(g);
            }
            else
            {
                g = new grunka(this, id);
                g.Location = calcGrunkLocation(g);
            }

            g.addAnotherGrunka += new EventHandler(addAnotherGrunka);
            g.thisIsDeleted += new EventHandler(deleteThisGrunka);

            this.grpBox.Controls.Add(g);
            grunkor.Add(g);

            if (id != -1)
            {
                sortGrunkor();
            }
            if (grunkor.Count > 0)
            {
                btn_removeFirst.Enabled = true;
                btn_removeLast.Enabled = true;
            }
            setSizeOfGroupBox();
        }

        public void addGrunkaAtOrder(int order)
        {
            increaseOrder(order);

            setLocationOfGrunkor();

            updateIdsOfGrunkor();

            addGrunka(order+1);
        }

        private void addAnotherGrunka(object sender, EventArgs e)
        {
            addGrunkaAtOrder((sender as grunka).order);
        }

        private void deleteThisGrunka(Object sender, EventArgs e)
        {
            grunkor.Remove(sender as grunka);
            (sender as grunka).Dispose();
            setIdsOfGrunkor();
            setLocationOfGrunkor();

            if (grunkor.Count <= 1)
            {
                btn_removeFirst.Enabled = false;
                btn_removeLast.Enabled = false;
            }
            setSizeOfGroupBox();
        }

        public void sortGrunkor()
        {
            grunkor.Sort(delegate(grunka p1, grunka p2)
            { return p1.getOrder().CompareTo(p2.getOrder()); });
        }

        private void setIdsOfGrunkor()
        {
            for (int i = 0; i < grunkor.Count; i++)
            {
                grunkor[i].order = i;
                grunkor[i].setOrderOnGroupboxText(i);
            }
        }

        private void updateIdsOfGrunkor()
        {
            foreach (grunka g in grunkor)
            {
                g.setOrderOnGroupboxText(g.order);
            }
        }

        private void setLocationOfGrunkor()
        {
            foreach (grunka g in grunkor)
            {
                g.Location = calcGrunkLocation(g);
            }
        }

        private void increaseOrder(int start)
        {
            for (int i = start+1; i < grunkor.Count; i++)
            {
                grunkor[i].order++;
            }
        }

        private void btn_addLast_Click(object sender, EventArgs e)
        {
            addGrunka();
        }

        private System.Drawing.Point calcGrunkLocation(grunka g)
        {
            if (isHorizontal)
                return new System.Drawing.Point(grpBox.Location.X + (g.getOrder() * g.Size.Width) + 5, 15);
            else
                return new System.Drawing.Point(5, grpBox.Location.X + (g.getOrder() * (g.Size.Height-5)) + 15);
        }

        private void btn_removeFirst_Click(object sender, EventArgs e)
        {
            deleteThisGrunka(grunkor[0], e);
        }

        private void btn_removeLast_Click(object sender, EventArgs e)
        {
            deleteThisGrunka(grunkor[grunkor.Count-1], e);
        }

        private void radioHorizontal_CheckedChanged(object sender, EventArgs e)
        {
            this.changeOrientation(this, e);
            if((sender as RadioButton).Checked == true){
                setSizeHorizontal();
            }
        }

        private void radioVertical_CheckedChanged(object sender, EventArgs e)
        {
            this.changeOrientation(this, e);
            if ((sender as RadioButton).Checked == true)
            {
                setSizeVertical();
            }
        }

        private void setSizeOfGroupBox()
        {
            if (isHorizontal)
            {
                if(grunkor.Count != 0)
                    grpBox.Size = new Size((grunkor.Count * 160) + 15, 80 + 25);
                 else
                    grpBox.Size = new Size(180, 80 + 5);

                this.Size = new Size(Math.Max(grpBox.Size.Width + 10,275), grpBox.Size.Height + 70);
            }
            else
            {
                if (grunkor.Count != 0)
                    grpBox.Size = new Size(160+15, (grunkor.Count * grunkor[0].Size.Height)+20);
                else
                    grpBox.Size = new Size(180, 80 + 5);

                this.Size = new Size(grpBox.Size.Width + 100, grpBox.Size.Height + 70);
            }
        }

        private void setSizeHorizontal()
        {
            isHorizontal = true;
            setSizeOfGroupBox();
            setLocationOfGrunkor();
        }

        private void setSizeVertical()
        {
            isHorizontal = false;
            setSizeOfGroupBox();
            setLocationOfGrunkor();
        }

    
    }
}
